<?php
namespace app\admin\controller;
use app\admin\logic\RegionLogic;

/**
 * 区域管理api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/18
 * Time: 14:35
 */
class Region extends Base {

    public function __construct(){
        parent::__construct();
        $this->r_logic = new RegionLogic();
    }

    /**
     * 获取区域
     * @return \think\response\Json
     */
    public function ajax_get_region(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->r_logic->get_search_region_maps($params['branchid'],$params['floorid']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('region')->get_list($maps,'id,floorid,name,sort,is_cp_ws','sort asc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['is_cp_ws_zw'] = $v->is_cp_ws_name;

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$v['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branchid'] = $b_detail['id'];
            $list[$k]['branch_zw'] = $b_detail['name'];
        }

        $total_num = model('region')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->r_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增区域
     * @return \think\response\Json
     */
    public function ajax_add_region(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增区域规则
        $r = $this->r_logic->check_add_region_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $r_list = $params['r_data'];
        $r_data = [];
        foreach($r_list as $k => $v){
            $r_data[$k]['floorid'] = $params['floorid'];
            $r_data[$k]['name'] = $v['name'];
            $r_data[$k]['is_cp_ws'] = $v['is_cp_ws'];
            $r_data[$k]['sort'] = $v['sort'];
        }

        $r = model('region')->insertAll($r_data);
        if($r){
            return json($this->r_logic->get_response(1,'新增成功'));
        }
        return json($this->r_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改区域
     * @return \think\response\Json
     */
    public function ajax_save_region(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改区域规则
        $r = $this->r_logic->check_save_region_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $r_detail = $r['data']['r_detail'];

        $r_data = [];
        $r_data['floorid'] = $params['floorid'];
        $r_data['name'] = trim($params['name']);
        $r_data['is_cp_ws'] = $params['is_cp_ws'];
        $r_data['sort'] = trim($params['sort']);

        $maps = [];
        $maps[] = ['id','=',$r_detail['id']];
        $r = model('region')->force()->save($r_data,$maps);
        if($r !== false){
            return json($this->r_logic->get_response(1,'修改成功'));
        }
        return json($this->r_logic->get_response(0,'修改失败'));
    }

    /**
     * 删除区域
     * @return \think\response\Json
     */
    public function ajax_del_region(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('region')->destroy($params['regionid']);
        if($r){
            return json($this->r_logic->get_response(1,'删除成功'));
        }
        return json($this->r_logic->get_response(0,'删除失败'));
    }
}